﻿/*******************************************************************************
 * Copyright © 2016-2020 北京网非科技有限公司 版权所有
 * Author: 北京网非科技有限公司
 * Description: 网非快速开发平台NET
 * Website：http://www.wfdot.com
*********************************************************************************/
using CubicFWNet.Common;
using CubicFWNet.Data.SystemManage.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CubicFWNet.Data.SystemManage
{
    public class UserRepository : RepositoryBase<User>
    {
        public IEnumerable<User> LoadUsers(int pageindex, int pagesize)
        {
            return this.IQueryable().OrderBy(u => u.Id).Skip((pageindex - 1) * pagesize).Take(pagesize);
            //return dbcontext.Users.OrderBy(u => u.Id).Skip((pageindex - 1) * pagesize).Take(pagesize);
        }

        /// <summary>
        /// 查询某角色包含的所有用户
        /// </summary>
        /// <param name="roleId">角色ID</param>
        /// <returns></returns>
        public IEnumerable<User> LoadInRole(Guid roleId)
        {   
            var result = from user in this.IQueryable()
                         where (
                             dbcontext.Set<Relevance>().AsQueryable().Where(uo => roleId == uo.SecondId && uo.Type == "UserRole")
                             .Select(u => u.FirstId)
                             .Distinct()
                         )
                         .Contains(user.Id)
                         select user;

            return result;
        }

     
        public int GetUserCntInRole(Guid roleId)
        {
            return LoadInRole(roleId).Count();
        }
        public IEnumerable<User> LoadInRole(int pageindex, int pagesize, Guid roleId)
        {
            return LoadInRole(roleId).OrderBy(u => u.Id).Skip((pageindex - 1) * pagesize).Take(pagesize);
        }
    }
}
